package leetcode_1_200;

import java.util.HashMap;
import java.util.Map;

public class LeeCode_169 {
    public static void main(String[] args) {
        System.out.println(majorityElement(new int[]{3, 2, 3}));
        System.out.println(majorityElement(new int[]{2,2,1,1,1,2,2}));
    }
    private static int majorityElement(int[] nums) {
        Map<Integer, Integer> map = new HashMap<>();
        int count = nums.length / 2;
        for (int i = 0; i < nums.length; i++) {
            map.put(nums[i], map.containsKey(nums[i]) ? map.get(nums[i]) + 1 : 1);
            if (map.get(nums[i]) > count){
                return nums[i];
            }
        }
        return 0;
    }
}
